package com.yzg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yzg.mapper.ProductAttributeMapper;
import com.yzg.pojo.ProductAttribute;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yzg.mapper.ProductAttributeCategoryMapper;
import com.yzg.pojo.ProductAttributeCategory;
import com.yzg.service.ProductAttributeCategoryService;
import org.springframework.util.StringUtils;

/**
* @Author: decade
* @Description: ${description}
* @Date: 2021/1/12 13:13
**/
@Service
public class ProductAttributeCategoryServiceImpl implements ProductAttributeCategoryService{
    @Autowired
    private ProductAttributeCategoryMapper productAttributeCategoryMapper;
    @Autowired
    private ProductAttributeMapper productAttributeMapper;

    /*查找所有的商品属性分类及其所属的所有商品属性*/
    @Override
    public List<ProductAttributeCategory> findAttrCateALl() {
        return productAttributeCategoryMapper.findAttrCateALl();
    }

    //获取商品属性分类
    @Override
    public List<ProductAttributeCategory> getList(IPage<ProductAttributeCategory> pageParam) {
         productAttributeCategoryMapper.selectPage(pageParam,null);
        return pageParam.getRecords();
    }

    //商品属性分类编辑
    @Override
    public int update(ProductAttributeCategory productAttrcate) {
        return productAttributeCategoryMapper.updateById(productAttrcate);
    }

    //商品属性分类添加
    @Override
    public int addProductAttrCate(ProductAttributeCategory productAttrcate) {
        return productAttributeCategoryMapper.insert(productAttrcate);
    }

    //商品属性分类删除
    @Override
    public int delete(Long id) {
        //先删除对应商品属性
        QueryWrapper<ProductAttribute> wrapper = new QueryWrapper<>();
        wrapper.eq("product_attribute_category_id",id);
        List<ProductAttribute> productAttributeList = productAttributeMapper.selectList(wrapper);
        List<Long> ids = new ArrayList<>();
        if(productAttributeList!=null&&productAttributeList.size()!=0){
            productAttributeList.forEach(productAttribute -> {
                ids.add(productAttribute.getId());
            });

            if(ids!=null&&ids.size()!=0) {
                productAttributeMapper.deleteBatchIds(ids);
            }
        }

        //再删除商品属性分类
        return productAttributeCategoryMapper.deleteById(id);
    }

    //根据id获取商品属性分类
    @Override
    public ProductAttributeCategory getProductAttrCateById(Long id) {
        return productAttributeCategoryMapper.selectById(id);
    }
}
